/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author anhtuan
 */
public class Util {

    /**
     * Generate Id
     * @return String
     */
    public static String generateUUID() {
        UUID uudi = UUID.randomUUID();
        return String.valueOf(uudi);
    }

    /**
     * Generate md5 string
     * @param str
     * @return String
     */
    public static String generateMD5(String str) {
        String hashword = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(str.getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
            hashword = hash.toString(16);
        } catch (NoSuchAlgorithmException nsae) {
        }
        return hashword;
    }
    public static String getYMD(String ddmmyyyy) {
        String yMD = "";
        String[] temps = ddmmyyyy.split("/");
        yMD = temps[2] + "-" + temps[1] + "-" + temps[0];
        return yMD;
    }

    /**
     * Convert date from YYYY-MM-DD to DD/MM/YYYY
     * @param ddmmyyyy
     * @return String
     */
    public static String getDMY(String yyyymmdd) {
        String dmy = "";
        String[] temps = yyyymmdd.split("-");
        //dd             MM              yyyy
        dmy = temps[2] + "/" + temps[1] + "/" + temps[0];
        return dmy;
    }

    public static String getDateTimeNow() {
        //get Date time System
        Date todayD = new Date(System.currentTimeMillis());
        //Format Date time
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //parse Date time to String.
        String todayS = dayFormat.format(todayD.getTime());
        return todayS;
    }

    /**
     * Get Current Date now
     * @return String
     */
    public static String getDateNow() {
        //get Date time System
        Date todayD = new Date(System.currentTimeMillis());
        //Format Date time
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
        //parse Date time to String.
        String todayS = dayFormat.format(todayD.getTime());
        return todayS;
    }

    /**
     * Split Time over Date time
     * @param time
     * @return String
     */
    public static String getTime(String time) {
        String time2 = "";
        String[] temps = time.split(":");
        time2 = temps[0] + ":" + temps[1] + ":" + temps[2];
        return time2;
    }

    public static String getDMYandTime(String datetime) {
        String dmy = "";
        String[] temps = datetime.split(" ");
        String ymd = temps[0];
        String time = temps[1];
        String time1 = getTime(time);
        dmy = getDMY(ymd);
        return dmy + " " + time1;
    }

     public static String getYMDandTime(String datetime) {
        String dmy = "";
        String[] temps = datetime.split(" ");
        String ymd = temps[0];
        String time = temps[1];
        dmy = getYMD(ymd);
        return dmy + " " + time;
    }
    /**
     * Get request url redirected after login
     * @param request
     * @return String
     */
    public static String getRedirectUrl(HttpServletRequest request) {
        String path = request.getServletPath();
        String url = request.getQueryString();
        return path + "?" + url;
    }
}
